package cn.SiFuLan.controls;

import cn.SiFuLan.dao.IbrandDao;
import cn.SiFuLan.dao.IspuDao;
import cn.SiFuLan.dao.impl.brandDaoImpl;
import cn.SiFuLan.dao.impl.spuDaoImpl;
import cn.SiFuLan.entity.brandEntity;
import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(urlPatterns = "/admin/bupdate1.shtml")
public class bupdate2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        brandEntity brand=new brandEntity();
        IbrandDao ibrandDao=new brandDaoImpl();
        SmartUpload smart=new SmartUpload();
        smart.setCharset("utf-8");
        smart.initialize(this.getServletConfig(),request,response);
        try {
            smart.upload();
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }
        File file=smart.getFiles().getFile(0);
            if(file.isMissing()){
                brand.setImageAddress(smart.getRequest().getParameter("img"));
            }else {
                String newname=new Date().getTime()+"."+file.getFileExt();
                String path=request.getSession().getServletContext().getRealPath("/upload");
                try {
                    file.saveAs(path+"/"+newname);
                } catch (SmartUploadException e) {
                    throw new RuntimeException(e);
                }
                brand.setImageAddress(newname);
            }
            String id=smart.getRequest().getParameter("brandid");
            if(id!=null &&!id.equals("")){
                brand.setBrandid(Integer.parseInt(id));
            }
            brand.setBrand_Name(smart.getRequest().getParameter("title"));
            brand.setInitial(smart.getRequest().getParameter("gg"));
            brand.setBrand_Info(smart.getRequest().getParameter("jq"));
            brand.setUpdateTime(new Date());
            ibrandDao.update(brand);
            request.getRequestDispatcher("bupdate3.do").forward(request,response);



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
